//
//  YahooStockBrandUpdater.m
//  MarketDataColloctor
//
//  Created by jiro on 10/10/12.
//  Copyright 2010 jiro music. All rights reserved.
//

#import "NikkeiStockBrandUpdater.h"
#import "../../../Commons/Commons.h"
#import "../../../DbTypes/DbTypes.h"
#import "../WebResourceCollector.h"


@implementation NikkeiStockBrandUpdater

+ (void) updateBrandName
{
	//	---- 存在する銘柄コードを取得
	StockBrandJaList* brands = [ MarketJaDbOperator allBrandCodes: YES ];
	// ----

	//	---- 該当htmlを取得する
	NSEnumerator* e = [ brands objectEnumerator ];
	StockBrandJa* brand;
	DbOperator* dbOperator = [ DbOperator defaultOperator ];
	while ( YES ) {
		NSMutableArray* urls = [ NSMutableArray array ];
		NSMutableArray* htmls = [ NSMutableArray array ];
		for ( int i = 0; i < 20; i++ ) {
			brand = [ e nextObject ];
			if ( brand == nil ) {
				break;
			}
			NSURL* url = [ NSURL URLWithString: [ NSString stringWithFormat:
				@"http://company.nikkei.co.jp/index.aspx?scode=%@",
				brand.code ] ];
			[ urls addObject: url ];
		}
		id collector = [ [ WebResourceCollector alloc ] initWithYahooEnigmaOption: NO htmlEncoding: NSShiftJISStringEncoding ];
		[ collector collectContentsOf: urls intoStrings: htmls ];

		//	---- htmlを解析しコードと企業名を取得
		NSEnumerator* htmlEnum = [ htmls objectEnumerator ];
		NSString* html;
		while ( html = [ htmlEnum nextObject ] ) {
//			NSLog( @"%@", html );
			NSError* error = nil;
			NSXMLDocument* document = [ [ NSXMLDocument alloc ] initWithXMLString: html options: NSXMLDocumentTidyHTML error: & error ];

			NSXMLElement* element = [ document rootElement ];
			NSXMLNode* node = element;
			
			NSError* err;
			NSArray* nodes = 
				[ node nodesForXPath:
					@"/html/body/div/div/div/div/table[@id=\"mainAreaTbl\"]/tr[2]/td/table/tr[2]/"
					@"td[@class=\"leftProperty\"]/p[@class=\"code\"]" error: & err ];
			if ( [ nodes count ] == 0 ) {
				continue;
			}
			node = [ nodes objectAtIndex: 0 ];
			id code = [ [ node stringValue ] stringByTrimmingCharactersInSet: [ NSCharacterSet whitespaceAndNewlineCharacterSet ] ];

			nodes = 
				[ node nodesForXPath:
					@"/html/body/div/div/div/div/table[@id=\"mainAreaTbl\"]/tr[2]/td/table/tr[2]/"
					@"td[@class=\"barandName\"]/p" error: & err ];
			if ( [ nodes count ] == 0 ) {
				NSLog( @"fail to get name for code: %@", code );
			}
			node = [ nodes objectAtIndex: 0 ];
			id name = [ [ node stringValue ] stringByTrimmingCharactersInSet: [ NSCharacterSet whitespaceAndNewlineCharacterSet ] ];

			NSLog( @"%@: %@", code, name );

			//	---- 企業名を更新
			NSString* sql = [ NSString stringWithFormat: 
				@"update %@ set %@ = '%@' where %@ = '%@';",
				kText_StockBrandJaTable, kText_Name, name, kText_Code, code, kText_Name ];
			PGresult* res = nil;
			if ( DbResponse_Success != [ dbOperator executeQuery: sql intoResult: & res ] ) {
				NSLog( @"企業名の更新に失敗: %@", sql );
				NSLog( @"code: %@, name: %@", code, name );
			}
			//	----
		}
		//	----

		if ( brand == nil ) {
			break;
		}
	}
	//	----
		
	//	---- 後始末
	[ dbOperator disconnect ];
}

@end
